package com.linzp.bishe.filter;

import com.linzp.bishe.common.TokenContent;
import com.linzp.bishe.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName JwtAuthenticationTokenFilter
 * @Description token过滤器 验证token有效性
 * @Author LinZePeng
 * @Date 2021/12/26 18:41
 * @Version 1.0
 */
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

	@Autowired
	private TokenContent tokenContent;

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
		LoginUser loginUser = tokenContent.getLoginUser(request);
		if (loginUser != null){
			tokenContent.verifyToken(loginUser);
			request.setAttribute("account",loginUser.getAccount());
		}
		filterChain.doFilter(request,response);
	}
}
